package com.x.service.serviceImpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.x.entity.po.AddBlogPO;
import com.x.entity.po.UpdBlogPO;
import com.x.entity.pojo.Blog;
import com.x.entity.vo.ListBlogVO;
import com.x.mapper.BlogMapper;
import com.x.result.Result;
import com.x.service.BlogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @description
 * @Author Lin
 * @Date 2021/5/21 16:22
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    BlogMapper blogMapper;

    /**
     * 发布博客功能
     * @param addBlogPO
     * @return 影响行数
     */
    @Override
    public Result insertBlog(AddBlogPO addBlogPO) {

        Blog blog = new Blog();
        BeanUtils.copyProperties(addBlogPO, blog);

        int row = blogMapper.insert(blog);
        if (row == 0){
            return new Result(null , Result.ON, "插入失败");
        }

        return new Result(null, Result.OK, "插入成功");
    }

    /**
     * 批量删除博客
     * @param ids 博客ids
     * @return
     */
    @Override
    public Result deleteBlog(ArrayList<Integer> ids) {

        int row = blogMapper.deleteBatchIds(ids);
        if (row == 0){
            return new Result(null, Result.ON, "删除失败");
        }

        return new Result(null, Result.OK, "删除成功");
    }

    /**
     * 修改博客
     * @param updBlogPO
     * @return
     */
    @Override
    public Result updateBlog(UpdBlogPO updBlogPO) {

        Blog blog = new Blog();

        BeanUtils.copyProperties(updBlogPO, blog);

        int row = blogMapper.updateById(blog);
        if (row == 0){
            return new Result(null, Result.ON, "修改失败");
        }

        return new Result(null, Result.OK, "修改成功");
    }

    /**
     * 分页查询所有博客
     * @param pageNo 页号
     * @param pageSize 页面大小
     * @return
     */
    @Override
    public Result listBlog(Integer pageNo, Integer pageSize) {

        Page<Blog> page = new Page<>(pageNo, pageSize);
        Page<Blog> blogPage = blogMapper.selectPage(page, null);

        List<Blog> blogs = blogPage.getRecords();
        long pageTotal = blogPage.getTotal();

        ArrayList<ListBlogVO> listBlogVOs = new ArrayList<>();

        for (Blog blog : blogs) {
            ListBlogVO listBlogVO = new ListBlogVO();
            BeanUtils.copyProperties(blog, listBlogVO);

            Date time = blog.getUpdateTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = simpleDateFormat.format(time);
            listBlogVO.setDate(date);

            listBlogVOs.add(listBlogVO);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("listBlogVOs", listBlogVOs);
        data.put("pageTotal", pageTotal);

        return new Result(data, Result.OK, "请求成功");
    }
}











